package com.yinhai.asynctask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yinhai.dao.ContactGroupDaoImpl;
import com.yinhai.model.ContactGroup;
import com.yinhai.model.ContactsUser;
import com.yinhai.util.PhoneSearchFunction;
import com.yinhai.util.PinYinUtil;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class ContactGroupTask  extends AsyncTaskForHttpProcess{
	String retStr = null;
	ContactGroupDaoImpl contactGroupDao;
	public ContactGroupTask(Context context) {
		super(context,false);
	    contactGroupDao = new ContactGroupDaoImpl(context);
	}
	
	public ContactGroupTask(Context context, boolean autoForword) {
		super(context, autoForword);
		contactGroupDao = new ContactGroupDaoImpl(context);
	}

	public ContactGroupTask(Context context, int timeoutRequestCount,
			boolean autoForword) {
		super(context, timeoutRequestCount, autoForword);
		contactGroupDao = new ContactGroupDaoImpl(context);
	}
	@Override
	protected void parse(Map<String, Object> result) {
		this.retStr = (String)result.get(AsyncTaskContext.RESPONSE_CONTENT);
		try {
			JSONObject jsonObject = new JSONObject(retStr);
			JSONArray ja = (JSONArray)((JSONObject)jsonObject.get("obj")).get("data");
			JSONObject jo = null;
			ContactGroup contactGroup = null;
			List<ContactGroup> list = new ArrayList<ContactGroup>();
			for(int i = 0; i < ja.length(); i++) {
				jo = ja.getJSONObject(i);
				contactGroup = new ContactGroup();
				//主键自动生成
				contactGroup.setId(jo.get("recid").toString().trim());
				contactGroup.setGroupId(jo.get("recid").toString().trim());
				contactGroup.setName(jo.getString("ctgroupname"));
				contactGroup.setPeopleNume(jo.get("membernum")==null?0:Integer.parseInt(jo.get("membernum").toString()));
				contactGroup.setPy(PinYinUtil.getFirstSpell(contactGroup.getName()));
				contactGroup.setFirstpy(PinYinUtil.getFirstSpell(contactGroup.getName()));
				contactGroup.setFullPY(PinYinUtil.getFullSpell(contactGroup.getName()));
				
				contactGroup.setNameSimple(PhoneSearchFunction.GetSearchStr(contactGroup.getName()));
				
				contactGroup.setFullpynum(PinYinUtil.getString2Num(PinYinUtil.getFullSpell(contactGroup.getName())));
				contactGroup.setFullpysep(PinYinUtil.getFullSpellSep(contactGroup.getName()));
				contactGroup.setFullpynumsep(PinYinUtil.getString2Num(PinYinUtil.getFullSpellSep(contactGroup.getName())));
				contactGroup.setParent_id(jo.get("parent_id").toString().trim());
//				long k = contactGroupDao.insert(contactGroup);
				list.add(contactGroup);
				if((i%1000==0 && i !=0)||(i == (ja.length()-1))){
					SQLiteDatabase db = this.context.openOrCreateDatabase("im.db", Context.MODE_PRIVATE, null);  
			        db.beginTransaction();  
			        
			        for (int j = 0; j < list.size(); j++) {  
			        	contactGroup= list.get(j);
			        	 db.execSQL("INSERT INTO contact_group(id, groupid, name,people_num,py,full_py,first_py,name_simple,full_py_num,full_py_sep,full_py_num_sep,parent_id)" +
			        	 		" values(?, ?, ?,?,?,?,?,?,?,?,?,?)" ,  
			                     new Object[]{contactGroup.getId(),contactGroup.getGroupId(),contactGroup.getName(),contactGroup.getPeopleNume(),contactGroup.getPy(),
			        			 contactGroup.getFullPY(),contactGroup.getFirstpy(),contactGroup.getNameSimple(),contactGroup.getFullpynum(),contactGroup.getFullpysep(),contactGroup.getFullpynumsep(),contactGroup.getParent_id() });
			        }  
			        db.setTransactionSuccessful();  
			        db.endTransaction(); 
			        
			        
			     //  if(db !=null){
			    	   db.close();
			    	   list.clear();
			      // }
				}
				
			}
			 
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
